package org.water.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.water.base.service.BaseService;
import org.water.base.support.BeanQuery;
import org.water.common.param.AppSysParam;
import org.water.sys.entity.SysResource;
import org.water.sys.repository.ResourceDao;
/**
 * 
 * @author qzy
 *
 */
@Service
public class ResourceService extends BaseService<SysResource, String> {

	
	protected ResourceDao getResourceDao()
	{
		return (ResourceDao) this.baseRespository;
	}
	/**
	 * 查询可用的资源
	 * @param resourceId
	 * @return
	 */
	public SysResource findUsableResource(String resourceId)
	{
		SysResource resource;
		BeanQuery<SysResource> beanQuery=this.baseRespository.createBeanQuery(SysResource.class);
		beanQuery.eq("id", resourceId).eq("delStatus", 0).eq("useStatus", 1);
		resource=beanQuery.one();
		return resource;
	}
	/**
	 * 根据父结点查询子资源
	 * @param resourceId
	 * @return
	 */
	public List<SysResource> findByParent(String parentResourceId)
	{
		List<SysResource> resources=null;
		SysResource parent=new SysResource();
		parent.setId(parentResourceId);
		resources=getResourceDao().findByParentAndDelStatus(parent,0);
		return resources;
	}
	/**
	 * 查询所有资源(部分字段)
	 * @return
	 */
	public List<SysResource> findAllResource()
	{
		List<SysResource> resources=null;
		BeanQuery<SysResource> beanQuery=this.baseRespository.createBeanQuery(SysResource.class).eq("delStatus", 0);
		beanQuery.eq("useStatus", 1);
		beanQuery.select("id","name","parent");
		resources=beanQuery.list();
		return resources;
	}
	/**
	 * 查询用户拥有菜单下的功能
	 * @param menu
	 * @return
	 */
	@Cacheable(value="userOwnResoucrceFun",key="#userId+#menu")
	public List<Map<String, Object>> findUserFunByMenu(String userId,String menu)
	{
		HashMap<String, Object> params=new HashMap<>();
		params.put("userId", userId);
		params.put("menu", menu);
		StringBuffer sql=new StringBuffer();
		sql.append(" select DISTINCT r.id,r.name,r.icon,r.url,r.img_url,r.identify,r.snum,r.type,r.parent_id  from sys_resource r,sys_role_resource rr, ");
		sql.append(" 		 (select ur.role_id from sys_user_role ur where 1=1 and ur.user_id=:userId ");
		sql.append(" 		  UNION ");
		sql.append(" 		 select gr.role_id from  sys_group g,sys_group_role gr,sys_group_user gu ");
		sql.append(" 		where 1=1 and g.id=gr.group_id and g.id=gu.group_id and gu.user_id=:userId ) rl ");
		sql.append(" 		where r.del_status=0 and r.use_status=1 and r.type=3 and r.id=rr.resource_id and rr.role_id=rl.role_id ");
		sql.append("            and r.parent_id in (select  id from sys_resource where identify=:menu)");
		sql.append(" 		 order by r.snum  ");
		List<Map<String, Object>> funs=this.getResourceDao().createNativeQueryToMap(sql.toString(), params);;
		return funs;
	}
	
	/**
	 * 查询用户拥有的资源
	 * @param userId
	 * @return
	 */
	@Cacheable(value="userOwnResoucrce",key="#userId")
	public List<Map<String, Object>> findResourceByUserId(String userId)
	{
		HashMap<String, Object> params=new HashMap<>();
		params.put("userId", userId);
		StringBuffer sql=new StringBuffer();
		sql.append(" select DISTINCT r.id,r.name,r.icon,r.url,r.img_url,r.identify,r.snum,r.type,r.parent_id  from sys_resource r,sys_role_resource rr, ");
		sql.append("  (select ur.role_id from sys_user_role ur where 1=1 and ur.user_id=:userId ");
		sql.append("   UNION ");
		sql.append("  select gr.role_id from  sys_group g,sys_group_role gr,sys_group_user gu ");
		sql.append(" where 1=1 and g.id=gr.group_id and g.id=gu.group_id and gu.user_id=:userId ) rl ");
		sql.append(" where r.del_status=0 and r.use_status=1 and r.type!=3 and r.id=rr.resource_id and rr.role_id=rl.role_id ");
		sql.append("  order by r.snum  ");
		return this.getResourceDao().createNativeQueryToMap(sql.toString(), params);
	}
	
	/**
	 * 查询用户拥有关联的接口
	 * @param userId
	 * @return
	 */
	@Cacheable(value="userOwnResoucrce",key="\"api\"+#userId")
	public List<Map<String, Object>> findApisByUserId(String userId)
	{
		HashMap<String, Object> params=new HashMap<>();
		params.put("userId", userId);
		String resourceRootId=AppSysParam.getProp("auth.menu.root");
		if(StringUtils.isEmpty(resourceRootId))
		{
			resourceRootId="root";
		}
		StringBuffer sql=new StringBuffer();
		sql.append(" select r.id,r.name,r.icon,r.url,r.img_url,r.identify,r.snum,r.type,r.parent_id,r.apis from sys_resource r where r.id='"+resourceRootId+"' ");
		sql.append(" union ");
		sql.append(" select DISTINCT r.id,r.name,r.icon,r.url,r.img_url,r.identify,r.snum,r.type,r.parent_id,r.apis  from sys_resource r,sys_role_resource rr, ");
		sql.append("  (select ur.role_id from sys_user_role ur where 1=1 and ur.user_id=:userId ");
		sql.append("   UNION ");
		sql.append("  select gr.role_id from  sys_group g,sys_group_role gr,sys_group_user gu ");
		sql.append(" where 1=1 and g.id=gr.group_id and g.id=gu.group_id and gu.user_id=:userId ) rl ");
		sql.append(" where r.del_status=0 and r.use_status=1  and r.id=rr.resource_id and rr.role_id=rl.role_id ");
		//sql.append("  order by r.snum  ");
		return this.getResourceDao().createNativeQueryToMap(sql.toString(), params);
	}
}
